/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RecordFrame.java
 *
 * Created on 2011-10-18, 21:32:14
 */
package com.lanchat.view;

import com.lanchat.model.data.LogInfo;
import com.lanchat.model.util.LocalSaver;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class RecordFrame extends javax.swing.JFrame {
    
    private ArrayList<String> userList;
    private LocalSaver saver;
    private ArrayList<LogInfo> infoList;
    /** Creates new form RecordFrame */
    public RecordFrame() {
        saver=new LocalSaver();
        saver.ReadList();
        infoList=saver.getInfoList();
        userList=new ArrayList<String>();
        initComponents();
        for(int i=0;i<infoList.size();i++){
            String tmp=infoList.get(i).getUser().getUserName();
            if(!userList.contains(tmp)){
                userList.add(tmp);
            }
        }
        DefaultListModel model=new DefaultListModel();
        for(int i=0;i<userList.size();i++){
            model.addElement(userList.get(i));
        }
        UserList.setModel(model);
        UserList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        UserList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                LogInfo tmp=null;
                String user=UserList.getSelectedValue().toString();
                //System.out.print(user);
                LogTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "内容", "时间"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

                    @Override
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

                    @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
                DefaultTableModel m=(DefaultTableModel)LogTable.getModel();
                m.setRowCount(0);
                for(int i=0;i<infoList.size();i++){
                    tmp=infoList.get(i);
                    if(tmp.getUser().getUserName() == null ? user == null : tmp.getUser().getUserName().equals(user)){
                        m.addRow(new String[]{tmp.getMsg(),tmp.getDate().toString()});
                    }
                }
            }
        });
        //this.setDefaultCloseOperation();
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        SearchField = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        UserList = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        LogTable = new javax.swing.JTable();
        SearchButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel2.setText("查找内容：");

        SearchField.setText("内容");

        jScrollPane3.setViewportView(UserList);

        LogTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "内容", "时间"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(LogTable);

        SearchButton.setText("查找");
        SearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("退出");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(SearchField, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(SearchButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(CancelButton)
                        .addGap(19, 19, 19))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(SearchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SearchButton)
                    .addComponent(CancelButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(54, 54, 54))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchButtonActionPerformed
        // TODO add your handling code here:
        String info=SearchField.getText();
        if(info.length()==0){
            JOptionPane.showMessageDialog(null, "查找内容不能为空！");
            return;
        }
        LogTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "用户名","IP地址","内容"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class,java.lang.String.class,java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        DefaultTableModel m=(DefaultTableModel)LogTable.getModel();
        m.setRowCount(0);
        for(int i=0;i<infoList.size();i++){
            LogInfo tmp=infoList.get(i);
            if(tmp.getMsg().indexOf(info)!=-1){
                if(tmp.getUser().getAddress() == null ? "" != null : !tmp.getUser().getAddress().equals("")){
                    m.addRow(new String[]{tmp.getUser().getUserName(),tmp.getUser().getAddress(),tmp.getMsg()});
                } else{
                    m.addRow(new String[]{tmp.getUser().getUserName(),"2",tmp.getMsg()});
                }

            }
        }
    }//GEN-LAST:event_SearchButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JTable LogTable;
    private javax.swing.JButton SearchButton;
    private javax.swing.JTextField SearchField;
    private javax.swing.JList UserList;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
}
